package com.briup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.briup.comon.BaseContext;
import com.briup.comon.R;
import com.briup.entity.AddressBook;
import com.briup.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: liao
 * @Date: 2022/9/14-09-14-16:55
 * @Description：地址簿管理
 */
@RequestMapping("/addressBook")
@Slf4j
@RestController
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        
        //先得到当前添加地址的用户id
        Long userId = BaseContext.getCurrentId();
        
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        
        return R.success("添加地址成功");

    }

    /**
     * 通过当前用户id查询所有地址信息
     * @param session
     * @return
     */
    
    
    @GetMapping("/list")
    public R<List<AddressBook>> getAll(HttpSession session){
        
        //通过session拿到当前用户的id
        Long sessionId = (Long) session.getAttribute("user");

        //通过当前用户id查询所有地址信息
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(AddressBook::getUserId,sessionId);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        
        if (list!=null){
          return  R.success(list);
        }
        
        return R.error("当前用户收货地址为空");
    }

    /**
     * 更改默认地址
     * @param addressBook
     * @return
     */
    
    
    @PutMapping("/default")
    public R<AddressBook> updateDefaultAddr(@RequestBody AddressBook addressBook){
        log.info("请求到更改默认地址{}",addressBook.getId());
        
        //首先先把该用户的所有地址设为0
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.set(AddressBook::getIsDefault,0);

        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(queryWrapper);
        
      addressBook.setIsDefault(1);
      addressBookService.updateById(addressBook);
      
      return R.success(addressBook);

    }

    /**
     * 通过地址的id查询当前的对象信息
     * @param id
     * @return
     */
    
    @GetMapping("/{id}")
    public R<AddressBook> getOne(@PathVariable Long id){

        AddressBook addressBook = addressBookService.getById(id);
        
        if (addressBook!=null){
            return R.success(addressBook);
        }


        return R.error("获取地址失败");
    }

    /**
     * 更改地址信息
     * @param addressBook
     * @return
     */
    
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        
        
        addressBookService.updateById(addressBook);
        
        return R.success("更新成功！");
    }

    /**
     * 通过地址id删除地址
     * @param ids
     * @return
     */
    
    @DeleteMapping
    public R<String> delete(Long ids){
        
        addressBookService.removeById(ids);
        return R.success("删除成功！");
        
    }
    
    
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        //先查询出这个用户有几个地址
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }
    
    
    
    
    
}
